home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-24 | 14.5 KB | 463 lines | [TEXT/SPM ] |
-
- void InitToolbox()
- {
- InitGraf(&qd.thePort);
- FlushEvents(everyEvent, 0);
- MaxApplZone();
- MaxApplZone();
- }
-
-
- OSErr GetProcessData(StringPtr nameString, char* buffer, Size* bufferLength)
- {
- Boolean foundProcess;
- ProcessSerialNumber process;
- ProcessInfoRec infoRec;
- OSErr result;
- FSSpec processFSSpec;
- Str31 tempStr;
- int i;
- long tempNum;
-
- *bufferLength = 0;
-
- process.highLongOfPSN = 0;
- process.lowLongOfPSN = kNoProcess;
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = (StringPtr) tempStr;
- infoRec.processAppSpec = &processFSSpec;
-
- while ((result = GetNextProcess(&process)) == noErr)
- {
- result = GetProcessInformation(&process, &infoRec);
- if (result != noErr)
- break;
-
- if (EqualString((ConstStr255Param) infoRec.processName, (ConstStr255Param) nameString, false, true))
- break;
- }
-
- if (result == noErr)
- {
- i = 0;
-
- BlockMove("Process Info for \"", buffer, 24);
- i += 24;
- BlockMove(tempStr+1, buffer+i, tempStr[0]);
- i += tempStr[0];
- BlockMove("\"\n", buffer+i, 2);
- i += 2;
-
- BlockMove("\nProcess Serial Number: ", buffer+i, 24);
- i += 24;
- tempNum = (infoRec.processNumber.highLongOfPSN & 0xF0000000) >> 28;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.highLongOfPSN & 0x0F000000) >> 24;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.highLongOfPSN & 0x00F00000) >> 20;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.highLongOfPSN & 0x000F0000) >> 16;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.highLongOfPSN & 0x0000F000) >> 12;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.highLongOfPSN & 0x00000F00) >> 8;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.highLongOfPSN & 0x000000F0) >> 4;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = infoRec.processNumber.highLongOfPSN & 0x0000000F;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.lowLongOfPSN & 0xF0000000) >> 28;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.lowLongOfPSN & 0x0F000000) >> 24;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.lowLongOfPSN & 0x00F00000) >> 20;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.lowLongOfPSN & 0x000F0000) >> 16;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.lowLongOfPSN & 0x0000F000) >> 12;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.lowLongOfPSN & 0x00000F00) >> 8;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processNumber.lowLongOfPSN & 0x000000F0) >> 4;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = infoRec.processNumber.lowLongOfPSN & 0x0000000F;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
-
- BlockMove("\nProcess Type: ", buffer+i, 24);
- i += 24;
- BlockMove(&infoRec.processType, buffer+i, 4);
- i += 4;
-
- BlockMove("\nProcess Creator: ", buffer+i, 24);
- i += 24;
- BlockMove(&infoRec.processSignature, buffer+i, 4);
- i += 4;
-
- BlockMove("\nProcess Mode: ", buffer+i, 24);
- i += 24;
- tempNum = (infoRec.processMode & 0xF0000000) >> 28;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processMode & 0x0F000000) >> 24;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processMode & 0x00F00000) >> 20;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processMode & 0x000F0000) >> 16;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processMode & 0x0000F000) >> 12;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processMode & 0x00000F00) >> 8;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processMode & 0x000000F0) >> 4;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = infoRec.processMode & 0x0000000F;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
-
- BlockMove("\nProcess Location: ", buffer+i, 24);
- i += 24;
- tempNum = (((unsigned long) infoRec.processLocation) & 0xF0000000) >> 28;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (((unsigned long) infoRec.processLocation) & 0x0F000000) >> 24;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (((unsigned long) infoRec.processLocation) & 0x00F00000) >> 20;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (((unsigned long) infoRec.processLocation) & 0x000F0000) >> 16;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (((unsigned long) infoRec.processLocation) & 0x0000F000) >> 12;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (((unsigned long) infoRec.processLocation) & 0x00000F00) >> 8;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (((unsigned long) infoRec.processLocation) & 0x000000F0) >> 4;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = ((unsigned long) infoRec.processLocation) & 0x0000000F;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
-
- BlockMove("\nProcess Size: ", buffer+i, 24);
- i += 24;
- NumToString(infoRec.processSize, tempStr);
- BlockMove(tempStr+1, buffer+i, tempStr[0]);
- i += tempStr[0];
-
- BlockMove("\nProcess Free Mem: ", buffer+i, 24);
- i += 24;
- NumToString(infoRec.processFreeMem, tempStr);
- BlockMove(tempStr+1, buffer+i, tempStr[0]);
- i += tempStr[0];
-
- BlockMove("\nProcess Launched By: ", buffer+i, 24);
- i += 24;
- tempNum = (infoRec.processLauncher.highLongOfPSN & 0xF0000000) >> 28;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.highLongOfPSN & 0x0F000000) >> 24;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.highLongOfPSN & 0x00F00000) >> 20;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.highLongOfPSN & 0x000F0000) >> 16;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.highLongOfPSN & 0x0000F000) >> 12;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.highLongOfPSN & 0x00000F00) >> 8;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.highLongOfPSN & 0x000000F0) >> 4;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = infoRec.processLauncher.highLongOfPSN & 0x0000000F;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.lowLongOfPSN & 0xF0000000) >> 28;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x0F000000) >> 24;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x00F00000) >> 20;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x000F0000) >> 16;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x0000F000) >> 12;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x00000F00) >> 8;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x000000F0) >> 4;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
- tempNum = infoRec.processLauncher.lowLongOfPSN & 0x0000000F;
- *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
-
- /*
- BlockMove("\nProcess Launch Time: ", buffer+i, 24);
- i += 24;
- DateString((long) infoRec.processLaunchDate, abbrevDate, (StringPtr) tempStr, nil);
- BlockMove(tempStr+1, buffer+i, tempStr[0]);
- i += tempStr[0];
- BlockMove(" ", buffer+i, 2);
- i += 2;
- TimeString((long) infoRec.processLaunchDate, true, (StringPtr) tempStr, nil);
- BlockMove(tempStr+1, buffer+i, tempStr[0]);
- i += tempStr[0];
- */
-
- BlockMove("\nProcess Active Time: ", buffer+i, 24);
- i += 24;
- NumToString(infoRec.processActiveTime, tempStr);
- BlockMove(tempStr+1, buffer+i, tempStr[0]);
- i += tempStr[0];
- BlockMove(" ticks\n", buffer+i, 24);
-
- *bufferLength = i;
- }
-
- return result;
- }
-
-
- /*
- OSErr DoneReqParams(AppleEvent* appleEvent)
- {
- DescType typeCode;
- Size actualSize;
- OSErr err;
-
- err = AEGetAttributePtr(appleEvent, keyMissedKeywordAttr,
- typeWildCard, &typeCode, NULL, 0, &actualSize);
- if (err == errAEDescNotFound)
- return noErr;
- else if (err == noErr)
- return errAEEventNotHandled;
- else
- return err;
- }
- */
-
- Boolean gquit;
-
- OSErr HandleAEQuit(AppleEvent* quitAppleEvent, AppleEvent* reply,
- long handlerRefCon)
- {
- OSErr err;
-
- /*
- err = DoneReqParams(quitAppleEvent);
- if (err != noErr)
- return err; // set quit flag anyway?
- */
- gquit = true;
- return noErr;
- }
-
-
-
- void main()
- {
- OSErr err;
- AEEventHandlerUPP upp;
- EventRecord event;
- Boolean isOpen = false;
- enum {forInform, forRead, forWrite} waitingState;
- Size bufferLen;
- PPCOpenPBRec openpb;
- PPCInformPBRec informpb;
- PPCClosePBRec closepb;
- PPCAcceptPBRec acceptpb;
- PPCReadPBRec readpb;
- PPCWritePBRec writepb;
- PPCPortRec portName;
- char buffer[1024];
-
- InitToolbox();
-
- upp = NewAEEventHandlerProc(HandleAEQuit);
- err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- upp, 0L, false);
- if (err != noErr)
- {
- DebugStr("\pAEInstallEventHandler failed");
- return;
- }
- gquit = false;
-
- err = PPCInit();
- if (err <noErr)
- {
- DebugStr("\pPPCInit failed");
- return;
- }
-
- portName.nameScript = smSystemScript;
- BlockMove(portName.name, "\pBark", 5);
- portName.portKindSelector = ppcByCreatorAndType;
- portName.u.port.creator = 'BæRK';
- portName.u.port.type = 'BæRK';
- openpb.ioCompletion = NULL;
- openpb.serviceType = ppcServiceRealTime;
- openpb.resFlag = 0;
- openpb.portName = &portName;
- openpb.locationName = NULL;
- openpb.networkVisible = true;
- err = PPCOpen(&openpb, false);
- if (err < noErr)
- {
- DebugStr("\pPPCOpen failed");
- return;
- }
- isOpen = true;
-
- informpb.ioCompletion = NULL;
- informpb.portRefNum = openpb.portRefNum;
- informpb.autoAccept = true;
- informpb.portName = 0;
- informpb.locationName = NULL;
- informpb.userName = NULL;
- err = PPCInform(&informpb, true); // async!
- if (err < noErr)
- {
- DebugStr("\pPPCInform failed");
- closepb.ioCompletion = NULL;
- closepb.portRefNum = openpb.portRefNum;
- PPCClose(&closepb, false);
- return;
- }
- waitingState = forInform;
-
- // loop while polling :-)
- while (!gquit)
- {
- if (WaitNextEvent(highLevelEventMask, &event, 60, NULL))
- {
- err = AEProcessAppleEvent(&event);
- if (err != noErr)
- {
- DebugStr("\pAEProcessAppleEvent failed");
- break;
- }
- }
-
- if (waitingState == forInform)
- {
- if (informpb.ioResult != 1)
- {
- if (informpb.ioResult != noErr)
- {
- DebugStr("\pPPCInform failed on completion");
- break;
- }
-
- acceptpb.ioCompletion = NULL;
- acceptpb.sessRefNum = informpb.sessRefNum;
- err = PPCAccept(&acceptpb, false);
- if (err != noErr)
- {
- DebugStr("\pPPCAccept failed");
- break;
- }
-
- readpb.ioCompletion = NULL;
- readpb.sessRefNum = informpb.sessRefNum;
- readpb.bufferLength = 1024;
- readpb.bufferPtr = buffer;
- err = PPCRead(&readpb, true); // async!
- if (err != noErr)
- {
- DebugStr("\pPPCRead failed");
- break;
- }
-
- waitingState = forRead;
- }
- }
- else if (waitingState == forRead)
- {
- if (readpb.ioResult != 1)
- {
- if (readpb.ioResult != noErr)
- {
- DebugStr("\pPPCRead failed on completion");
- break;
- }
-
- // ok! get data and send it
- buffer[readpb.actualLength] = 0;
- c2pstr(buffer);
- DebugStr((StringPtr) buffer);
- err = GetProcessData((StringPtr) buffer, buffer, &bufferLen);
- if (err != noErr)
- {
- DebugStr("\pGetProcessData failed");
- break;
- }
-
- writepb.ioCompletion = NULL;
- writepb.sessRefNum = informpb.sessRefNum;
- writepb.bufferLength = bufferLen;
- writepb.bufferPtr = buffer;
- writepb.more = false;
- err = PPCWrite(&writepb, true);
- if (err != noErr)
- {
- DebugStr("\pPPCWrite failed");
- break;
- }
-
- waitingState = forWrite;
- }
- }
- else if (waitingState == forWrite)
- {
- if (writepb.ioResult != 1)
- {
- if (writepb.ioResult != noErr)
- {
- DebugStr("\pPPCWrite failed on completion");
- break;
- }
-
- // close, open & inform again
- isOpen = false;
- closepb.ioCompletion = NULL;
- closepb.portRefNum = openpb.portRefNum;
- err = PPCClose(&closepb, false);
- if (err != noErr)
- {
- DebugStr("\pPPCClose failed");
- break;
- }
-
- portName.nameScript = smSystemScript;
- BlockMove(portName.name, "\pBark", 5);
- portName.portKindSelector = ppcByCreatorAndType;
- portName.u.port.creator = 'BæRK';
- portName.u.port.type = 'BæRK';
- openpb.ioCompletion = NULL;
- openpb.serviceType = ppcServiceRealTime;
- openpb.resFlag = 0;
- openpb.portName = &portName;
- openpb.locationName = NULL;
- openpb.networkVisible = true;
- err = PPCOpen(&openpb, false);
- if (err < noErr)
- {
- DebugStr("\pPPCOpen failed");
- break;
- }
- isOpen = true;
-
- informpb.ioCompletion = NULL;
- informpb.portRefNum = openpb.portRefNum;
- informpb.autoAccept = true;
- informpb.portName = 0;
- informpb.locationName = NULL;
- informpb.userName = NULL;
- err = PPCInform(&informpb, true); // async!
- if (err < noErr)
- {
- DebugStr("\pPPCInform failed");
- break;
- }
- waitingState = forInform;
- }
- }
- }
-
- if (isOpen)
- {
- closepb.ioCompletion = NULL;
- closepb.portRefNum = openpb.portRefNum;
- PPCClose(&closepb, false);
- }
- }
-
-